// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: MIT
package utils

import (
        "testing"
)

func TestValidateURI(t *testing.T) {
        tests := []struct {
                name    string
                uri     string
                wantErr bool
        }{
                {
                        name:    "valid http URI",
                        uri:     "http://example.com",
                        wantErr: false,
                },
                {
                        name:    "valid https URI",
                        uri:     "https://example.com",
                        wantErr: false,
                },
                {
                        name:    "valid http URI with path",
                        uri:     "http://example.com/api/v1/data",
                        wantErr: false,
                },
                {
                        name:    "valid https URI with query parameters",
                        uri:     "https://example.com/api?param1=value1&param2=value2",
                        wantErr: false,
                },
                {
                        name:    "valid http URI with port",
                        uri:     "http://localhost:8080",
                        wantErr: false,
                },
                {
                        name:    "invalid URI - missing scheme",
                        uri:     "example.com",
                        wantErr: true,
                },
                {
                        name:    "invalid URI - empty string",
                        uri:     "",
                        wantErr: true,
                },
                {
                        name:    "invalid URI - malformed",
                        uri:     "http://[::1]:namedport",
                        wantErr: true,
                },
                {
                        name:    "unsupported scheme - ftp",
                        uri:     "ftp://example.com",
                        wantErr: true,
                },
                {
                        name:    "unsupported scheme - file",
                        uri:     "file:///etc/passwd",
                        wantErr: true,
                },
                {
                        name:    "unsupported scheme - ws",
                        uri:     "ws://example.com",
                        wantErr: true,
                },
                {
                        name:    "unsupported scheme - wss",
                        uri:     "wss://example.com",
                        wantErr: true,
                },
        }

        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
                        err := ValidateURI(tt.uri)
                        if (err != nil) != tt.wantErr {
                                t.Errorf("ValidateURI() error = %v, wantErr %v", err, tt.wantErr)
                        }

                        if err != nil && tt.wantErr {
                                t.Logf("Expected error: %v", err)
                        }
                })
        }
}

func TestValidateURI_EdgeCases(t *testing.T) {
        tests := []struct {
                name    string
                uri     string
                wantErr bool
        }{
                {
                        name:    "URI with IPv4 address",
                        uri:     "http://192.168.1.1:8080",
                        wantErr: false,
                },
                {
                        name:    "URI with IPv6 address",
                        uri:     "http://[2001:db8::1]:8080",
                        wantErr: false,
                },
                {
                        name:    "URI with user info",
                        uri:     "http://user:pass@example.com",
                        wantErr: false,
                },
                {
                        name:    "URI with fragment",
                        uri:     "http://example.com/path#section",
                        wantErr: false,
                },
                {
                        name:    "only scheme - invalid",
                        uri:     "http://",
                        wantErr: true,
                },
                {
                        name:    "scheme with double slash but no host - invalid",
                        uri:     "http:///path",
                        wantErr: true,
                },
                {
                        name:    "host with trailing dot - valid in DNS but check implementation",
                        uri:     "http://example.com.",
                        wantErr: false,
                },
        }

        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
                        err := ValidateURI(tt.uri)
                        if (err != nil) != tt.wantErr {
                                t.Errorf("ValidateURI() error = %v, wantErr %v", err, tt.wantErr)
                        }
                })
        }
}

func BenchmarkValidateURI(b *testing.B) {
        uris := []string{
                "http://example.com",
                "https://example.com/api/v1/data",
                "http://localhost:8080",
                "invalid-uri",
        }

        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                for _, uri := range uris {
                        _ = ValidateURI(uri)
                }
        }
}

